﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EBookStore.Orders
{
   
    /// <summary>
    /// Represents single book order.
    /// Each order is build from SingleBooksOrders
    /// </summary>
    public class SingleBookOrder:BookAndAmount
    {


        #region Properties
        List<BookStatusHistory> _statusHistory;

        /// <summary>
        /// Current book order status
        /// </summary>
        public eBookOrderStatus Status
        {
            get { return _statusHistory.Last().Status; }
        }

        /// <summary>
        /// Books statuses changes over time
        /// </summary>
        public List<BookStatusHistory> HistoryStatus
        {
            get { return _statusHistory; }
        }




        private int _suppliedToCustomer;

        /// <summary>
        /// How many copies were supplied to customer.
        /// </summary>
        public int SuppliedToCustomer
        {
            get { return _suppliedToCustomer; }
            set { _suppliedToCustomer = value; }
        }
        
        #endregion
         
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="Book"></param>
        /// <param name="amount"></param>
        public SingleBookOrder(Books.Book Book, int amount):
            base(Book,amount)
        {            
            _statusHistory = new List<BookStatusHistory>();
            _statusHistory.Add(new BookStatusHistory(DateTime.Now, eBookOrderStatus.OrderedByCustomer));            
            _suppliedToCustomer = 0;
            
        }


        #region Public Functions

        /// <summary>
        /// Update book's status
        /// </summary>
        /// <param name="status"></param>
        public void UpdateBookStatus(eBookOrderStatus status)
        {
            if ((int)status <= (int)Status ){
                throw new Exception("Can't set new status lower then previous");
            }
            _statusHistory.Add( new BookStatusHistory(DateTime.Now, status));
        }


        /// <summary>
        /// Recieves books from publisher and updates amount of books
        /// that left to deliver to customer.
        /// </summary>
        /// <param name="amountFromPublisher">Books recieved from publisher</param>
        /// <param name="AmountToDeliver">Amount of books needed to deliver to customer</param>
        /// <returns>Amount of books that left to deliver to customer</returns>
        public int UpdateBooksAmountSuppliedByPublisher(ref int amountFromPublisher, out int AmountToDeliver)
        {
            AmountToDeliver = 0;
            // we have missing books
            if ((this.SuppliedToCustomer < this.Amount) && amountFromPublisher > 0)
            {
                // more then we need
                if (amountFromPublisher >= (this.Amount - this.SuppliedToCustomer))
                {
                    int tmp = this.Amount - this.SuppliedToCustomer;
                    AmountToDeliver = tmp;
                    this.SuppliedToCustomer += tmp;
                    amountFromPublisher -= tmp;                    
                }
                else // less then we need
                {
                    this.SuppliedToCustomer += amountFromPublisher;
                    amountFromPublisher = 0;
                    AmountToDeliver = amountFromPublisher;
                }
            }
            return (this.Amount - this.SuppliedToCustomer);
        }

        #endregion

    }
}
